from flask import Blueprint, request
from utilsapp.mysql_con import db_connect, db_delete, db_operation, db_table_pagelist, db_update, db_insert

deviceType = Blueprint('deviceType', __name__)
tableName = 'device_type'

# 设备类型——查询
@deviceType.route('/getlist', methods=['POST'])
def table_list():
    if 'page' in request.json.keys() and 'size' in request.json.keys():
        page = request.json['page']
        size = request.json['size']
    else:
        page = 1
        size = 999
    j = db_table_pagelist(tableName, page, '', size)
    return j


@deviceType.route('/add', methods=['POST'])
def device_type_add():
    return db_insert(tableName, request.json)


@deviceType.route('/delete', methods=['POST'])
def device_type_delete():
    id = request.json['id']
    return db_delete(tableName, f'WHERE id=%s', {'id': id})

@deviceType.route('/edit', methods=['POST'])
def device_type_edit():
    id = request.json['id']
    keys_not_add = ['id']
    addModel = {key: request.json[key]
                for key in request.json if key not in keys_not_add}
    return db_update(tableName, addModel, f'WHERE id=%s', {"id": id})